package com.mrdeer.web.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mrdeer.common.JSONResult;
import com.mrdeer.common.entity.dto.DictDTO;
import com.mrdeer.common.entity.sys.Dict;
import com.mrdeer.common.entity.sys.DictData;
import com.mrdeer.common.tools.RedisTemplateHelper;
import com.mrdeer.web.service.sys.SysDictDataService;
import com.mrdeer.web.service.sys.SysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "字典管理接口")
@RequestMapping("/mrdeer/dict")
@Transactional
public class SysDictController {

    @Resource
    private SysDictService sysDictService;

    @Resource
    private SysDictDataService sysDictDataService;

    @Resource
    private RedisTemplateHelper redisTemplate;

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public JSONResult getAll() {
        List<Dict> list = sysDictService.getAll();
        return JSONResult.ok(list);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加")
    public JSONResult add(@RequestBody Dict dict) {
        if (sysDictService.getOne(new QueryWrapper<Dict>().eq("type", dict.getType())) != null) {
            return JSONResult.errorMsg("字典类型Type已经存在");
        }
        sysDictService.save(dict);
        return JSONResult.ok("添加成功");
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value = "编辑")
    public JSONResult edit(Dict dict) {
        Dict old = sysDictService.getById(dict.getId());
        //若type修改判断唯一
        if (!old.getType().equals(dict.getType()) && (sysDictService.getOne(new QueryWrapper<Dict>().eq("type", dict.getType())) != null)) {
            return JSONResult.errorMsg("字典类型Type已经存在");
        }
        sysDictService.updateById(dict);
        //删除缓存
        redisTemplate.delete("dictData::" + dict.getType());
        return JSONResult.ok("编辑成功");
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "通过ID删除")
    public JSONResult delAllByIds(@RequestParam String[] ids) {
        for (String id : ids) {
            Dict dict = sysDictService.getById(id);
            sysDictService.removeById(id);
            sysDictDataService.remove(new QueryWrapper<DictData>().eq("dict_id", id));
            //删除缓存
            redisTemplate.delete("dictData::" + dict.getType());
        }
        return JSONResult.ok("删除成功");
    }

    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @ApiOperation(value = "搜索字典")
    public JSONResult searchPermissionList(@RequestParam DictDTO dto) {
//        List<Dict> list = dictService.fuzzyQuery(dto);
        return JSONResult.ok(null);
    }
}
